package uno.rocc.framework.security.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import uno.rocc.common.constant.SecurityConstant;
import uno.rocc.framework.security.domain.RoccUnoUserDetails;
import uno.rocc.system.domain.dao.SysPermission;
import uno.rocc.system.domain.dao.SysUser;
import uno.rocc.system.domain.dao.SysUserPermission;
import uno.rocc.system.service.SysPermissionService;
import uno.rocc.system.service.SysUserPermissionService;
import uno.rocc.system.service.SysUserService;

import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class RoccUnoUserDetailsServiceImpl implements UserDetailsService {
    private final SysUserService sysUserService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        LambdaQueryWrapper<SysUser> query = new LambdaQueryWrapper<SysUser>().eq(SysUser::getUsername, username);
        SysUser sysUser = sysUserService.getOne(query);
        //用户不存在
        if (ObjectUtil.isNull(sysUser)) {
            throw new UsernameNotFoundException(SecurityConstant.USER_NO_EXISTS);
        }

        //返回对象信息
        return RoccUnoUserDetails.builder().sysUser(sysUser).build();
    }
}
